package com.pkk.penguin.frame.know.java.driven.aop.config;

import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * aop的切面编程
 *
 * @author peikunkun
 * @version V1.0
 * @date 2020-10-14 11:45
 **/
//告诉Spring容器这个是切面类
@Aspect
@Slf4j
public class AopAspect {

  /**
   * 切面公共引用方法
   *
   * @return void
   * @Param
   * @author peikunkun
   * @date 2020/10/14 0014 上午 11:47
   * @since
   */
  @Pointcut("execution(public int com.pkk.penguin.frame.know.java.driven.aop.handle.AopHandle.*(..))")
  public void pointCut() {
    log.info(this.getClass().getSimpleName() + "  【切入点】  执行了");
  }


  /**
   * 前置
   *
   * @return void
   * @Param joinPoint
   * @author peikunkun
   * @date 2020/10/14 0014 上午 11:57
   * @since
   */
  @Before("pointCut()")
  public void before(JoinPoint joinPoint) {
    log.info(this.getClass().getSimpleName() + "  【前置】 方法名:" + joinPoint.getSignature().getName() + ";参数:" + Arrays
        .asList(joinPoint.getArgs()) + "  执行了");
  }


  /**
   * 后置操作
   *
   * @return void
   * @Param joinPoint
   * @author peikunkun
   * @date 2020/10/14 0014 下午 1:47
   * @since
   */
  @After("pointCut()")
  public void after(JoinPoint joinPoint) {
    log.info(this.getClass().getSimpleName() + "  【后置】  方法名:" + joinPoint.getSignature().getName() + "  执行了");
  }


  /**
   * 结果返回
   *
   * @return void
   * @Param
   * @author peikunkun
   * @date 2020/10/14 0014 下午 2:21
   * @since
   */
  @AfterReturning(value = "pointCut()", returning = "result")
  public void afterReturn(Object result) {
    log.info(this.getClass().getSimpleName() + " 【结果返回】  结果:" + JSONObject.toJSONString(result) + "  执行了");
  }


  /**
   * 异常处理
   *
   * @return void
   * @Param
   * @author peikunkun
   * @date 2020/10/14 0014 下午 2:26
   * @since
   */
  @AfterThrowing(value = "pointCut()", throwing = "exception")
  public void exception(Exception exception) {
    log.info(this.getClass().getSimpleName() + "  【异常】 异常信息:" + exception + "   执行了");
  }

}
